package com.share.two.nio;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.Date;
import java.util.Scanner;

/**
 * channel.write(buffer) 发送完数据后，要想再次发送，得buffer.clear
 *
 */
public class Client {

	
	@SuppressWarnings("resource")
	public static void main(String[] args) throws Exception {
		SocketChannel sChannel = SocketChannel.open(new InetSocketAddress("127.0.0.1",12345));
		sChannel.configureBlocking(false);
		
		ByteBuffer buffer = ByteBuffer.allocate(1024);
		
		Scanner sc = new Scanner(System.in);
		
		while (sc.hasNext()) {
			String next = sc.next();
			buffer.put((new Date().toString() + "\n" + next).getBytes());
			System.out.println("client 发送： " + next);
			buffer.flip();
			sChannel.write(buffer);//同步的方式，发送成功后才会去执行下面代码
			buffer.clear();
		}
		
		sChannel.close();
		
	}
}
